package com.thread.base;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @User xiangyl
 * @Data 2020/8/22
 */
public class ThreadBase02 {

    private static Lock lock = new ReentrantLock();

    private static volatile Condition condition1 = lock.newCondition();


    public static void main(String[] args) {
        new Thread(() -> {
            try {
                System.out.println("start--");
                lock.lock();
                for (int i = 0; i < 10; i++) {
                    System.out.println("a=》" + i);
                }
                condition1.await();
                System.out.println("我是被b唤醒的");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }).start();

        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("b start");
                for (int i = 0; i < 10; i++) {
                    System.out.println("b-->" + i);
                }
                System.out.println("wake up a");
                condition1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }
}
